package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.JobListData;
import com.wbao.dianniu.utils.PictureDownloadUtils;

/**
 * 求职列表
 * 
 * @author yejianjun
 *
 */
public class JobListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<JobListData> mList;
	private Context mContext;
	
	public JobListAdapter(Context context) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mContext = context;
	}
	
	public void setData(List<JobListData> list) {
		mList = list;
	}
	
	public void addData(List<JobListData> list){
		if(null == mList){
			mList = new ArrayList<JobListData>();
		}
		for (int i = 0; i < list.size(); i++) {
			mList.add(list.get(i));
		}
	}
	
	public List<JobListData> getList(){
		return mList;
	}
	
	public void clear(){
		mList.clear();
		notifyDataSetChanged();
	}
	
	public void addTopData(JobListData data){
		mList.add(0, data);
	}
	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		GridHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.job_list_item, null);   
			holder = new GridHolder();
			holder.headIV = (ImageView)convertView.findViewById(R.id.user_head);
			holder.name = (TextView)convertView.findViewById(R.id.user_name);
			holder.sex = (ImageView)convertView.findViewById(R.id.user_sex);
			holder.label = (TextView)convertView.findViewById(R.id.user_label);
			holder.subjectTV =  (TextView)convertView.findViewById(R.id.job_list_item_subject);
			holder.salaryTV =  (TextView)convertView.findViewById(R.id.job_list_item_salary);
			holder.sexTV =  (TextView)convertView.findViewById(R.id.job_list_item_sex);
			holder.educationTV =  (TextView)convertView.findViewById(R.id.job_list_item_education);
			holder.experienceTV = (TextView) convertView.findViewById(R.id.job_list_item_experience);
			holder.addressTV = (TextView) convertView.findViewById(R.id.job_list_item_address);
			convertView.setTag(holder); 
		}else{
			holder = (GridHolder) convertView.getTag();
		}
		
		 JobListData data = mList.get(position);
		if(null != data){
			PictureDownloadUtils.displayHeadImage(mContext,holder.headIV, data.getHeadPic());
			holder.name.setText(data.getRealName() == null ? "": data.getRealName());
			holder.sex.setVisibility(View.GONE);
			holder.label.setText(data.getLabel() == null ? "": data.getLabel());
			holder.subjectTV.setText(data.getPost() == null ? "": data.getPost());
			holder.salaryTV.setText(data.getSalary()==null ? "" : "" + data.getSalary());
			holder.sexTV.setText(data.getSexDesc()  == null ? "": data.getSexDesc());
			holder.educationTV.setText(data.getEducation() == null ? "": data.getEducation());
			holder.experienceTV.setText(data.getExperience() == null ? "" : data.getExperience());
			holder.addressTV.setText(data.getAddress() == null ? "":"" + data.getAddress());
		}
		
		return convertView;
	}

	private class GridHolder {
		public ImageView headIV;
		public TextView name;
		public ImageView sex;
		public TextView label;
		public TextView subjectTV;
		public TextView salaryTV;
		public TextView sexTV;
		public TextView educationTV;
		public TextView experienceTV;
		public TextView addressTV;
	}
	
}
